#!/usr/bin/python
# -*- coding: utf-8 -*-

from trivmoo.utils.decorators import singleton
import unittest


# The simplest way to create a singleton is to use the singleton pattern
# defined by the singleton decorator


####################### Singleton unit test ############################
@singleton
class TestSingletonClass(object):
    def __init__(self):
        self.__string = ''    
    def set_string(self, a_string):
        self.__string = a_string        
    def get_string(self):
        return self.__string

class TestSingleton(unittest.TestCase):
    def test_creation(self):
        """TestSingleton: creation test"""
        oo1 = TestSingletonClass()
        oo2 = TestSingletonClass()
        oo3 = TestSingletonClass()        
        self.assertEqual(oo1, oo2)
        self.assertEqual(oo1, oo3)
        self.assertEqual(oo3, oo2)
    
    def test_set_and_get(self):
        """TestSingleton: creation test"""
        oo1 = TestSingletonClass()
        oo2 = TestSingletonClass()
        oo3 = TestSingletonClass()
        oo1.set_string("oo1")
        oo2.set_string("oo2")
        oo3.set_string("oo3")
        self.assertEqual(oo1.get_string(), oo2.get_string())
        self.assertEqual(oo1.get_string(), oo3.get_string())
        self.assertEqual(oo3.get_string(), oo2.get_string())
        
if __name__ == "__main__":
    unittest.main()